package pl.edu.agh.ki.ewywiadowka.client.windows.secretary;


import java.util.LinkedList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import pl.edu.agh.ki.ewywiadowka.client.Const;
import pl.edu.agh.ki.ewywiadowka.client.EWywiadowka;
import pl.edu.agh.ki.ewywiadowka.client.Const.Error.StudentModifyMsg;
import pl.edu.agh.ki.ewywiadowka.client.dto.AttendantDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.ClassDTO;
import pl.edu.agh.ki.ewywiadowka.client.dto.StudentDTO;

public class ModifyStudentWindow extends RegisterStudentAttendantWindow {
	
	private StudentDTO _student;

	public ModifyStudentWindow(EWywiadowka wywiadowka, StudentDTO student) {
		super(wywiadowka);
		
		_student = student;
		
		
		
		_nameTextBox.setText(student.name);
		_surnameTextBox.setText(student.surname);
//		_addressTextBox.setText(student.street);
//		_cityTextBox.setText(student.city);
//		_postalTextBox.setText(student.postal);
		_birthTextBox.setValue(student.birth);
		_birthPlaceTextBox.setText(student.birthPlace);
//		_attendantNameTextBox.setText(student.attendantName);
//		_attendantSurnameTextBox.setText(student.attendantSurname);
//		_attendantStreetTextBox.setText(student.attendantStreet);
//		_attendantCityTextBox.setText(student.attendantCity);
//		_attendantPostalTextBox.setText(student.attendantPostal);
//		_attendantTelephoneTextBox.setText(student.attendantTelephone.toString());
//		_attendantWorkTextBox.setText(student.attendantWork);
//		//_classTextBox.setSelectedIndex(_classes.indexOf());
//		_attendantLoginTextBox.setText(student.login);
//		_attendantPassTextBox.setText(student.pass);
		
		_okButton.setText("Modyfikuj");
		
	}
	
	public void studentModify() {
		
		ClassDTO clas = _classes.get(_classTextBox.getSelectedIndex());
		LinkedList<AttendantDTO> attendants = new LinkedList<AttendantDTO>();

		if (_tabSelectedNumber == 1) {
			Window
					.alert("Uzyj zakladki \"Isniejacy w systemie\" dla wyboru opiekunow ucznia");
			return;
		} else
			for (int i = 0; i < _attendants.size(); i++)
				if (_existingAttendantListBox.isItemSelected(i))
					attendants.add(_attendants.get(i));

		StudentDTO student = new StudentDTO(_surnameTextBox.getText(),
				_nameTextBox.getText(), Integer.parseInt(_numberTextBox
						.getText()), _addressTextBox.getText(), _birthTextBox
						.getValue(), _birthPlaceTextBox.getText(), attendants,
				clas);
		
		Const.eWywiadowkaService.modifyStudent(student, new AsyncCallback<StudentModifyMsg>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}

			@Override
			public void onSuccess(StudentModifyMsg result) {
				Window.alert(result.toString());
			}
			
		});
	}

	public void onClick(ClickEvent event) {
		Const.FormularMessages msg = null;
		if (event.getSource() == _okButton) {
		if ((msg = verifyFormular()) == Const.FormularMessages.OK)
			studentModify();
		else
			Window.alert(msg.toString());
		} else if (event.getSource() == _newAttendantButton) {
			if ((msg = verifyAttendantFormular()) == Const.FormularMessages.OK)
				attendantRegister();
			else
				Window.alert(msg.toString());
		}
	}
	
}
